<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/25
 * Time: 14:04
 */

namespace app\bis\controller;

/*
 * 商家入住申请注册
 */
use think\Controller;

class Register extends Controller
{
    //商户入住
    public function index()
    {
        //获取一级城市的数据
        $citys = model('city')->getNormalCitysByParentId();

        //获取一级栏目的数据
        $categorys = model('category')->getNormalCategoryByParentId();

        return $this->fetch('', [
            'citys' => $citys,
            'categorys' => $categorys,
        ]);
    }

    /*
     * 商户入住申请表单处理 ,
     * 数据入库：3张表的数据分别入库
     */
    public function add()
    {
        if (request()->isPost()){
            $data=input('post.');

            //bis数据校验
            $validate=validate('Bis');
            if (!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }

            // 获取经纬度
            $lnglat = \Map::getLngLat($data['address']);

            // 查询提交表单的用户是否存在
            $accountResult = Model('BisAccount')->get(['username' => $data['username']]);

            if ($accountResult) {
                $this->error('该用户存在，请重新分配');
            }

            // 商户基本信息入库,  添加操作o2o_bis表
            $bisData = [
                'name' => $data['name'],
                'city_id' => $data['city_id'],
                'city_path' => empty($data['se_city_id']) ? $data['city_id'] : $data['city_id'] . ',' . $data['se_city_id'],
                'logo' => $data['logo'],
                'licence_logo' => $data['licence_logo'],
                'description' => empty($data['description']) ? '' : $data['description'],
                'bank_info' => $data['bank_info'],
                'bank_user' => $data['bank_user'],
                'bank_name' => $data['bank_name'],
                'faren' => $data['faren'],
                'faren_tel' => $data['faren_tel'],
                'email' => $data['email'],
            ];
            $bisId = model('Bis')->add($bisData);
            //BisLocation数据校验
            $validate=validate('BisLocation');
            if (!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }

            $data['cat'] = '';
            if (!empty($data['se_category_id'])) {
                $data['cat'] = implode('|', $data['se_category_id']);
            }
            // 总店相关信息入库
            $locationData = [
                'bis_id' => $bisId,
                'name' => $data['name'],
                'logo' => $data['logo'],
                'tel' => $data['tel'],
                'contact' => $data['contact'],
                'category_id' => $data['category_id'],
                'category_path' => $data['category_id'] . ',' . $data['cat'],
                'city_id' => $data['city_id'],
                'city_path' => empty($data['se_city_id']) ? $data['city_id'] : $data['city_id'] . ',' . $data['se_city_id'],
                'api_address' => $data['address'],
                'open_time' => $data['open_time'],
                'content' => empty($data['content']) ? '' : $data['content'],
                'is_main' => 1,// 代表的是总店信息
                'xpoint' => empty($lnglat['result']['location']['lng']) ? '' : $lnglat['result']['location']['lng'],
                'ypoint' => empty($lnglat['result']['location']['lat']) ? '' : $lnglat['result']['location']['lat'],
            ];

            $locationId = model('BisLocation')->add($locationData);

            //BisAccount数据校验
            $validate=validate('BisAccount');
            if (!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }

            $data['code'] = mt_rand(100, 10000);
            $accounData = [
                'bis_id' => $bisId,
                'username' => $data['username'],
                'code' => $data['code'],
                'password' => md5($data['password'] . $data['code']),
                'is_main' => 1, // 代表的是总管理员
            ];

            $accountId = model('BisAccount')->add($accounData);

            if (!$accountId) {
                $this->error('申请失败');
            }

            // 发送邮件
            $url = request()->domain().url('bis/register/waiting', ['id'=>$bisId]);
            $title = "商城入驻申请通知";
            $content = "您提交的入驻申请需等待平台方审核，您可以通过点击链接<a href='".$url."' target='_blank'>查看链接</a> 查看审核状态";
            \phpmailer\Email::send($data['email'],$title, $content);  // 线上关闭 发送邮件服务

            $this->success('申请成功', url('register/waiting', ['id' => $bisId]));

        }
    }

    /**
     * 商家申请后等待页面
     */
    public function waiting($id) {
        if(empty($id)) {
            $this->error('error');
        }
        $detail = model('Bis')->get($id);

        return $this->fetch('',[
            'detail' => $detail,
        ]);
    }

}